package com.veryUsedCar.shiro;

import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {

    @Bean   //嵌入过滤器
    public ShiroFilterFactoryBean shiroFilterFactoryBean(@Autowired DefaultWebSecurityManager defaultWebSecurityManager){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();

        Map<String, String> map = new LinkedHashMap<>();
        // map的第一个参数 代表地址   拦截/不拦截  value
        /**
         * anon : 表示不需要登录,  放过
         * authc : 表示当前地址必须登录才可以访问
         * logout : 退出登录
         * perms : 必须要授权之后才可以进行访问
         */
        //key  url 路径 , value : 过滤器  (url是拦截还是不拦截 )
        map.put("/login/**", "anon");
        map.put("**", "authc");
        //退出登录
        map.put("/login/logout", "logout");
        // 需要授权
        /*map.put("/roleController/list", "perms[role-list]");
        map.put("/personController/toAddPerson", "perms[person_add]");
        map.put("/XgTbCourseDetails/list", "perms[course-list]");*/

        /*map.put("/**", "authc");*/
        shiroFilterFactoryBean.setLoginUrl("/reception/login");
        // 当没有权限时调用的方法
        shiroFilterFactoryBean.setUnauthorizedUrl("/reception/unauthorizedUrl");

        shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager);

        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
        return shiroFilterFactoryBean;
    }

    @Bean //安全控制器  控制realm 和拦截器交换
    public DefaultWebSecurityManager defaultWebSecurityManager(@Autowired com.veryUsedCar.shiro.LoginRealm loginRealm){
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        defaultWebSecurityManager.setRealm(loginRealm);
        return  defaultWebSecurityManager;
    }

    @Bean
    public com.veryUsedCar.shiro.LoginRealm loginRealm(@Autowired HashedCredentialsMatcher hashedCredentialsMatcher){
        com.veryUsedCar.shiro.LoginRealm loginRealm = new com.veryUsedCar.shiro.LoginRealm();
        //不能省略......
        loginRealm.setCredentialsMatcher(hashedCredentialsMatcher);
        return loginRealm;
    }

    @Bean
    public HashedCredentialsMatcher hashedCredentialsMatcher(){
        //shiro自带的哈希加密方式
        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
        //选择哈希加密方式
        hashedCredentialsMatcher.setHashAlgorithmName("MD5");
        //加密的迭代次数3次
        hashedCredentialsMatcher.setHashIterations(3);
        //1234 + sdfghjsdfgh  防止相同用户使用相同密码
        //设置编码
        hashedCredentialsMatcher.setStoredCredentialsHexEncoded(true);
        return hashedCredentialsMatcher;
    }
    //控制到按钮
    /*@Bean
    public ShiroDialect shiroDialect(){
        return new ShiroDialect();
    }*/
}
